<template>
  <!-- 弹框 -->
  <el-dialog title="请填写驳回原因" v-model="dialogVisible" width="50%" :before-close="handleClose">
    <el-form :model="formData" ref="formRef" :rules="rules" label-width="120px">
      <el-form-item prop="reason">
        <el-input v-model="formData.reason" type="textarea" :rows="4" maxlength="200" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleAuditError">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from "vue";

const emit = defineEmits(["update:visible", "update:auditDesc"]);
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});
const dialogVisible = ref(props.visible);
// 关闭弹框的逻辑
const handleClose = () => {
  dialogVisible.value = false;
  formData.value.reason = "";
  emit("update:visible", false);
};
watchEffect(() => {
  dialogVisible.value = props.visible;
});

const formData = ref({
  reason: "",
});
const rules = ref({
  reason: [{ required: true, message: "请输入驳回原因", trigger: "blur" }],
});
const formRef = ref(null);
const handleAuditError = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log(formData.value);
      emit("update:auditDesc", formData.value.reason);
      handleClose();
    } else {
      console.log("error submit!!");
    }
  });
};
</script>

<style lang="scss" scoped>
:deep(.el-form-item__content) {
  margin-left: 0 !important;
}

.margin-top {
  margin-top: 20px;
}
</style>
